package com.winderinfo.yikaotianxia.ui.setup;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseActivity;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.SPUtils;
import com.winderinfo.yikaotianxia.core.utils.ToastUtil;
import com.winderinfo.yikaotianxia.ui.register.RegisterBean;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class AlterActivity extends BaseActivity implements Contract.View {

    @BindView(R.id.mo_oldpwd)
    EditText moOldpwd;
    @BindView(R.id.mo_pwd)
    EditText moPwd;
    @BindView(R.id.mo_qpwd)
    EditText moQpwd;
    private Presenter presenter;
    Map<String, Object> map = new HashMap<>();
    private SPUtils spUtils;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_modifypwd;
    }

    @Override
    protected void initView() {
        //实例P
        presenter = new Presenter(this);
        spUtils = new SPUtils(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onSuccess(Object data) {
        //修改密码数据
        if (data instanceof RegisterBean) {
            //修改失败
            if (((RegisterBean) data).getCode() == 500) {
                ToastUtil.showSuccess(this, ((RegisterBean) data).getMsg());
            }
            //修改成功
            if (((RegisterBean) data).getCode() == 0) {
                ToastUtil.showSuccess(this, ((RegisterBean) data).getMsg());
            }
        }
    }

    @Override
    public void onFailure(Object error) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }


    @OnClick({R.id.mo_back, R.id.mo_wc})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.mo_back:
                finish();
                break;
            case R.id.mo_wc:
                String oldpwd = moOldpwd.getText().toString();
                String pwd = moPwd.getText().toString();
                String qpwd = moQpwd.getText().toString();
                //判断空
                if (TextUtils.isEmpty(oldpwd) || TextUtils.isEmpty(pwd) || TextUtils.isEmpty(qpwd)) {
                    ToastUtil.showSuccess(this, "不能为空");
                    return;
                }
                //判断密码是否一致
                if (!pwd.equals(qpwd)) {
                    ToastUtil.showSuccess(this, "密码不一致");
                    return;
                }
                //请求修改密码数据
                /**
                 * @phone 手机号
                 * @oldpassword 旧密码
                 * @password 新密码
                 * */
                map.clear();
                map.put("phone", spUtils.getString("Phone", ""));
                map.put("oldpassword", oldpwd);
                map.put("password", qpwd);
                presenter.onPostRequest(Api.EDITPWD, map, RegisterBean.class);
                break;
        }
    }
}
